﻿Imports Co9320.NativeMethods
Imports Microsoft.Win32

Class MainWindow

    Private Sub Image_MouseLeftButtonDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
        DragMove()
    End Sub

    Private Sub Image_MouseWheel(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseWheelEventArgs)
        If Me.Width + e.Delta \ 10 >= 25 Then
            Me.Width += e.Delta \ 10
            Me.Height += e.Delta \ 10
        End If
    End Sub

    Private Sub SetImage(ByVal uri As Uri)

        Dim src As ImageSource
        src = New BitmapImage(uri)
        If src.Width > 1 OrElse src.Height > 1 Then
            MainImage.Source = src
            My.Settings.PathUri = uri.ToString
            My.Settings.Save()
        End If

    End Sub


    Private Sub MainImage_PreviewDrop(ByVal sender As System.Object, ByVal e As System.Windows.DragEventArgs)
        SetImage(New Uri(e.Data.GetData(DataFormats.FileDrop)(0)))
    End Sub


    Private Sub PathMenuItem_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)

        Me.Topmost = False

        Dim dialog = New OpenFileDialog With { _
            .Filter = "PNGファイル(*.png)|*.png|すべてのファイル(*.*)|*.*"}

        If dialog.ShowDialog Then
            Try
                SetImage(New Uri(dialog.FileName))
            Catch ex As Exception
                ' 無視
            End Try
        End If

        Me.Topmost = True

    End Sub

    Private Sub MainWindow_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded


        Dim handle = New System.Windows.Interop.WindowInteropHelper(Me).Handle
        Dim style = GetWindowLong(handle, GWL_EXSTYLE)
        SetWindowLong(handle, GWL_EXSTYLE, style Or WS_EX_TOOLWINDOW)

        If My.Settings.PathUri.Trim = "" Then
            Exit Sub
        End If

        Try
            SetImage(New Uri(My.Settings.PathUri))
        Catch ex As Exception
            ' 無視
        End Try
    End Sub

    Private Sub ExitMenuItem_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Me.Close()
    End Sub

    Private Sub CloneMenuItem_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Dim w = New Co9320.MainWindow
        w.Show()
    End Sub

    Private Sub ReverseMenuItem_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        If MainImage.FlowDirection = FlowDirection.RightToLeft Then
            MainImage.FlowDirection = FlowDirection.LeftToRight
        Else
            MainImage.FlowDirection = FlowDirection.RightToLeft
        End If
    End Sub

End Class
